function [table, score, scaled_score, linesNum, weightedTable, tetrominoLand] = ...
            performMove(table, piece, position, rotation)

[table_max_height, table_max_width] = size(table);

score_table = [0, 100, 300, 500, 800];
linesNum = 0;

heights = (size(table, 1) : -1 : 1)';
heights = heights(:, ones(size(table, 2), 1));
table_h = max(heights .* table, [], 1);
tetromino_h = getPieceHeight(table_h, piece, position, rotation);

tetromino = generateTetromino(piece, rotation);

%pronađi oznaku centra i zamaskiraj ga
[tetromino_c_row tetromine_c_col] =  find(tetromino == 2);
tetromino(tetromino_c_row, tetromine_c_col) = 1;

tetromino_s = size(tetromino);

%pozicije u koje treba umetnuti novi blok
t_row = table_max_height - tetromino_h - tetromino_c_row + 2 : ...
    table_max_height - tetromino_h - tetromino_c_row + tetromino_s(1) + 1;
t_col = position - tetromine_c_col + 1 : ...
    position - tetromine_c_col + tetromino_s(2);

tetrominoLand = table_max_height - t_row(end) + 1;

if min(t_row) >= 1
    %ako blok stane na ploču dodaj novi
    table(t_row, t_col) = table(t_row, t_col) + tetromino;
    
    %provijeri na kojim lokacijama je ispunjen cijeli red
    nonfull_row_i = find(sum(table, 2) < table_max_width);
    %dodijeli bodove
    nonfull_row_num = length(nonfull_row_i);
    
    linesNum = table_max_height - nonfull_row_num;
    score = score_table(linesNum + 1);

    newTable = zeros(table_max_height, table_max_width);
    newTable(table_max_height - nonfull_row_num + 1 : end, :) = table(nonfull_row_i, :);
    table = newTable;
    
else
    %smanji blok na veličinu koja stane na ploču
    shrink_s = find(t_row <= 1);
    shrink_s = shrink_s(end);
    shrinked_tetromino = tetromino(shrink_s:end,:);
    t_row = t_row(shrink_s:end);
    if t_row ~=0
        %ako stane barem jedan dio bloka
        table(t_row, t_col) = table(t_row, t_col) + shrinked_tetromino;
    end
   
    score = -800; 
    
end

scaled_score = score / 800;

heights = (size(table, 1) : -1 : 1)';
heights = heights(:, ones(size(table, 2), 1));
weightedTable = heights .* table;



function [ piece_h ] = getPieceHeight( table_h, piece, position, rotation )
%za zadani blok i ploču određuje na kojoj visini treba biti dodani novi
%blok
switch piece
    case 1
        piece_h = max(table_h(position), ...
            table_h(position + 1)) +2;
    case 2
        switch rotation
            case 1
                piece_h = max(table_h(position), ...
                    table_h(position - 1)) +2;
            case 2
                piece_h = max(table_h(position -1), ...
                    max(table_h(position), table_h(position + 1))) +1;
            case 3
                piece_h = max([table_h(position) + 2, ...
                    table_h(position + 1)]);                  
            case 4
                piece_h = max([table_h(position-1), ...
                    table_h(position), table_h(position+1)+1]) +1;
        end
    case 3
        switch rotation
            case 1
                piece_h = max(table_h(position), ...
                    table_h(position + 1)) +2;
            case 2
                piece_h = max([table_h(position-1)+1, ...
                    table_h(position), table_h(position+1)]) +1;
            case 3
                piece_h = max([table_h(position - 1), ...
                    table_h(position) + 2]);
            case 4
                piece_h = max([table_h(position -1), ...
                    table_h(position), table_h(position + 1)]) +1;
        end
    case 4
        if rotation == 1
            piece_h = max(table_h(position) + 1,...
                table_h(position - 1) +2 );
        else
            piece_h = max([table_h(position - 1),...
                table_h(position) + 1, table_h(position + 1) + 1]);
        end
    case 5
        if rotation == 1
            piece_h = max(table_h(position -1) + 1, ...
                table_h(position) + 2);
        else
            piece_h = max([table_h(position - 1) + 1, ...
                table_h(position) + 1, table_h(position + 1)]);
        end
    case 6
        if rotation == 1
            piece_h = max([table_h(position - 1), ...
                table_h(position), table_h(position + 1), ...
                table_h(position + 2)]) + 1;
        else
            piece_h = table_h(position) + 3;
        end
    case 7
        switch rotation
            case 1
                piece_h = max([table_h(position - 1) + 1, ...
                    table_h(position) + 2, table_h(position + 1) + 1]);
            case 2
                piece_h = max(table_h(position - 1) + 1, ...
                    table_h(position) + 2);
            case 3
                piece_h = max([table_h(position -1), table_h(position), ...
                    table_h(position + 1)]) + 1;
            case 4
                piece_h = max(table_h(position) + 2, ...
                    table_h(position +1) +1);
        end
end


